package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.FinanceModule.entity.MonthStatementEntity;
import org.jeecg.modules.FinanceModule.service.impl.MonthStatementsServiceImpl;
import org.jeecg.modules.zonglzp.service.WebMonthStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/WebMonthStatement")
public class WebMonthStatementController {
    @Autowired
    WebMonthStatementService service;

    @Autowired
    MonthStatementsServiceImpl Monthservice;
    //查询该用户所有总账单
    @GetMapping("/getWebMonthStatement")
    public JSONObject getWebMonthStatement(
            @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "token") String token
    ){
        JSONObject obj = new JSONObject();
        Page<MonthStatementEntity> page=new Page<MonthStatementEntity>(pageNo,pageSize);
        IPage<MonthStatementEntity> pageList=service.getMonthStatementList(page,token);
        obj.put("data",pageList);
        return obj;
    }


    /**
     * 在前端查询该用户某个总账单下的一条条月结账单
     */
    @GetMapping("/getsmallWebMonthStatement")
    public JSONObject smallMonthStatementList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "billid") String billid,
            String orderSn
    ){
        JSONObject obj = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Page<MonthStatementEntity> page=new  Page<MonthStatementEntity>(pageNo,pageSize);
        IPage<MonthStatementEntity> pageList=Monthservice.getsmallMonthStatementList(page,billid, orderSn);
        if(pageList!=null){
            for(int i=0;i<pageList.getRecords().size();i++){

                if(pageList.getRecords().get(i).getIsAfter()==0){
                    pageList.getRecords().get(i).setIsAfterString("正常订单");
                }else{
                    pageList.getRecords().get(i).setIsAfterString("售后订单");
                }


                if( pageList.getRecords().get(i).getOrderType()==0){
                    pageList.getRecords().get(i).setOrderTypeString("面料订单");
                }else  if( pageList.getRecords().get(i).getOrderType()==1){
                    pageList.getRecords().get(i).setOrderTypeString("个订订单");
                }else  if( pageList.getRecords().get(i).getOrderType()==2){
                    pageList.getRecords().get(i).setOrderTypeString("团订订单");
                }
                pageList.getRecords().get(i).setCreateTimeString(sdf.format(  pageList.getRecords().get(i).getCreateTime()));

            }
        }
        obj.put("data",pageList);
         return obj;
    }


    /**
     * 支付账单 负债支付
     */
    @GetMapping("/fuzhaizhifu")
    public JSONObject fuzhaizhifu(
            @RequestParam(name = "billId") String billId,
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "billMoney") String billMoney,
            @RequestParam(name = "accountBalance") String accountBalance



    ){
        JSONObject obj = new JSONObject();

        try {


        //用户余额
        BigDecimal yue=new BigDecimal(accountBalance);
        //账单金额
        BigDecimal zhangdanMoney=new BigDecimal(billMoney);
        if(yue.compareTo(zhangdanMoney)==-1){
            obj.put("code",1);
            obj.put("msg","对不起,您的余额小于账单金额");
            return obj;
        }
        int count=service.fuzhaizhifu(billId,userId,billMoney,accountBalance);
            obj.put("code",0);
            obj.put("msg","支付成功");
        }catch (Exception e){
            e.printStackTrace();
            obj.put("code",1);
            obj.put("msg","支付失败");
        }
        return obj;
    }

}
